export interface StorageBase {
  /**
   *
   *
   * @param {string} key
   * @param {*} value
   * @memberof StorageBase
   */
  setItem(key: string, value: any): void;
  getItem<T>(key: string): T | null;
  removeItem(key: string): void;
  clear(): void
}

class LocStorage implements StorageBase {
  setItem(key: string, value: any) {
    if (typeof value !== 'string') {
      value = JSON.stringify(value);
    }
    localStorage.setItem(key, value);
  }
  getItem<T>(key: string): T | null {
    let value: any = localStorage.getItem(key) || '';
    if (value.indexOf('{') !== -1) {
      return <T>JSON.parse(value);
    } else {
      return value;
    }
  }
  removeItem(key: string) {
    localStorage.removeItem(key);
  }
  clear(){
    localStorage.clear();
  }
}

class SesStorage implements StorageBase {
  setItem(key: string, value: any) {
    if (typeof value !== 'string') {
      value = JSON.stringify(value);
    }
    sessionStorage.setItem(key, value);
  }
  getItem<T>(key: string): T | null {
    let value: any = sessionStorage.getItem(key) || '';
    if (value.indexOf('{') !== -1) {
      return <T>JSON.parse(value);
    } else {
      return value;
    }
  }
  removeItem(key: string) {
    sessionStorage.removeItem(key);
  }
  clear(){
    sessionStorage.clear()
  }
}
export const locStorage = new LocStorage();
export const sesStorage = new SesStorage();
